package validation;

import org.openqa.selenium.WebElement;

import com.thoughtworks.selenium.SeleneseTestBase;

public class Tools {
	
	public static final int DEFAULT_WAIT= 10;
	public static final int DEFAULT_TIMEOUT= 15;
	private enum IsMethod {
		IS_ENABLED, IS_DISPLAYED, IS_SELECTED
	}

	@SuppressWarnings("rawtypes")
	private static final boolean isExecuted(Enum isMethod, WebElement element) {
		long timer = 0;
		while (true) {
			try {
				if ((IsMethod.IS_ENABLED.equals(isMethod) && element.isEnabled())
						|| (IsMethod.IS_DISPLAYED.equals(isMethod) && element.isDisplayed())
						|| (IsMethod.IS_SELECTED.equals(isMethod) && element.isSelected())) {
					return true;
				}
				else {
					Thread.sleep(DEFAULT_WAIT);
				}
			} catch (Exception e) {
			}
			timer += DEFAULT_WAIT;
			if (timer > DEFAULT_TIMEOUT) {
				break;
			}
		}
		return false;
	}

	public static final boolean isDisplayed(WebElement element) {
		return isExecuted(IsMethod.IS_DISPLAYED, element);
	}
	public static final void assertEquals(String expected, String actual) {
		SeleneseTestBase.assertEquals(expected, actual);
	}
}
